//
//  VisitorView.swift
//  微博
//
//  Created by admin on 2018/3/20.
//  Copyright © 2018年 xm. All rights reserved.
//

import UIKit

/**
 *  访客视图
 **/
class VisitorView: UIView {

    //圆圈
    lazy var circleImageView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //半透图
    lazy var maskImageView:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_mask_smallicon"))
    //大图标
    lazy var logoImageView:UIImageView = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
    //注册按钮
    lazy var registerBtn:UIButton = UIButton(title: "注册", fontSize: normalFontSize,
                                             normalColor: UIColor.darkGray, hightlightedColor: UIColor.orange,
                                             backgroundImgname: "common_button_white", target: nil, action: nil)
    //注册按钮
    lazy var loginBtn:UIButton = UIButton(title: "登录", fontSize: normalFontSize,
                                             normalColor: UIColor.darkGray, hightlightedColor: UIColor.orange,
                                             backgroundImgname: "common_button_white", target: nil, action: nil)
    
    lazy var messageLabel:UILabel=UILabel(text: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜",
                                          textColor: UIColor.darkGray, fontSize: normalFontSize, maxWeight: 230)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        initView()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置访客视图信息
    func setupVisitorViewInfo(dict:[String:String]?){
        
        if let img = dict?["imgName"], let message = dict?["message"] {
            logoImageView.image = UIImage(named:img)
            messageLabel.text = message
            if img.contains("visitordiscover_feed_image_house") {//判断是否为首页
                //首页
                startCircleImageViewanim()
            }else{
                //隐藏转动圆圈
                circleImageView.isHidden = true
            }
        }
    }
    
    private func startCircleImageViewanim(){
       let anim=CABasicAnimation(keyPath: "transform.rotation")
       anim.toValue=2 * Double.pi
       anim.repeatCount=MAXFLOAT
       anim.duration=15
       //动画完成不删，如果circleImageView被释放，动画会一起销毁
       anim.isRemovedOnCompletion=false;//防止动画进入后台后，在回来不动了
        //添加动画到图层
       circleImageView.layer.add(anim, forKey: nil)
    }

}


extension VisitorView{

    func initView(){
    
        backgroundColor = UIColor(hex: 0xEDEDED)
        
        //添加视图
        addSubview(circleImageView)
        addSubview(maskImageView)
        addSubview(logoImageView)
        addSubview(messageLabel)
        addSubview(registerBtn)
        addSubview(loginBtn)
        
        messageLabel.textAlignment = .center//设置文字居中显示
        
        //取消autoresizing
        for v in subviews {
           v.translatesAutoresizingMaskIntoConstraints=false
        }
        //代码设置自动布局
        //添加一条约束
        //转圈图布局
        addConstraint(NSLayoutConstraint(item: circleImageView, attribute: .centerX,
                                         relatedBy: .equal, toItem: self, attribute: .centerX,
                                         multiplier: 1.0, constant: 0))
        addConstraint(NSLayoutConstraint(item: circleImageView, attribute: .centerY,
                                         relatedBy: .equal, toItem: self, attribute: .centerY,
                                         multiplier: 1.0, constant: -60))
        //logo图标布局
        addConstraints([NSLayoutConstraint(item: logoImageView, attribute: .centerX,
                                           relatedBy: .equal, toItem: circleImageView,
                                           attribute: .centerX, multiplier: 1.0, constant: 0),
                        NSLayoutConstraint(item: logoImageView, attribute: .centerY,
                                           relatedBy: .equal, toItem: circleImageView,
                                           attribute: .centerY, multiplier: 1.0, constant: 0)
                        ])
        //消息label布局
        addConstraints([NSLayoutConstraint(item: messageLabel, attribute: .centerX,
                                           relatedBy: .equal, toItem: circleImageView,
                                           attribute: .centerX, multiplier: 1.0, constant: 0),
                        NSLayoutConstraint(item: messageLabel, attribute: .top,
                                           relatedBy: .equal, toItem: circleImageView,
                                           attribute: .bottom, multiplier: 1.0, constant: 20)
            ])
       //固定宽度在236个点
       addConstraint(NSLayoutConstraint(item: messageLabel, attribute: .width,
                                         relatedBy: .equal, toItem: nil,
                                         attribute: .notAnAttribute, multiplier: 1.0, constant: 236))
       //注册按钮布局
       addConstraints([NSLayoutConstraint(item: registerBtn, attribute: .left,
                                          relatedBy: .equal, toItem: messageLabel, attribute: .left,
                                          multiplier: 1.0, constant: 0),
                       NSLayoutConstraint(item: registerBtn, attribute: .top,
                                          relatedBy: .equal, toItem: messageLabel, attribute: .bottom,
                                          multiplier: 1.0, constant: 20),
                       NSLayoutConstraint(item: registerBtn, attribute: .width,
                                          relatedBy: .equal, toItem: nil,
                                          attribute: .notAnAttribute, multiplier: 1.0, constant: 100)
                       ])
    
        //登录按钮布局
        addConstraints([NSLayoutConstraint(item: loginBtn, attribute: .right,
                                           relatedBy: .equal, toItem: messageLabel, attribute: .right,
                                           multiplier: 1.0, constant: 0),
                        NSLayoutConstraint(item: loginBtn, attribute: .top,
                                           relatedBy: .equal, toItem: messageLabel, attribute: .bottom,
                                           multiplier: 1.0, constant: 20),
                        NSLayoutConstraint(item: loginBtn, attribute: .width,
                                           relatedBy: .equal, toItem: nil,
                                           attribute: .notAnAttribute, multiplier: 1.0, constant: 100)
            ])
        
        //遮罩图像布局
        let viewDict=["maskImageView":maskImageView,
                      "registerBtn":registerBtn] as [String : Any]
        //views 是VFL中的控件名称和实际名称的映射关系
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat:"H:|-0-[maskImageView]-0-|",
                                                      options: [], metrics: nil,
                                                      views: viewDict))
        addConstraints(NSLayoutConstraint.constraints(withVisualFormat:"V:|-0-[maskImageView]-(-35)-[registerBtn]",
                                                      options: [], metrics: nil,
                                                      views:viewDict))
        
        loginBtn.addTarget(self, action: #selector(onLoginBtnClick), for: .touchUpInside)
        registerBtn.addTarget(self, action: #selector(onRegistBtnClick), for: .touchUpInside)
    }
    
    func onLoginBtnClick() -> () {
        //发出登录通知
        NotificationCenter.default.post(name: NSNotification.Name(WBGoToLoginNotification), object: nil)
    }
    func onRegistBtnClick() -> () {
        
    }
}
